﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Moon.Sql
{
    public class UnitOfWork : IUnitOfWork
    {
        public IMoonSql[] Contexts { get; private set; }
        public IsolationLevel IsolationLevel { get; set; }
        private TransactionFactory factory = null;

        public UnitOfWork(IMoonSql[] contexts)
        {
            this.Contexts = contexts;
            factory = TransactionFactory.Create(contexts);
        }

        public void Begin()
        {
            factory.BeginTransaction(IsolationLevel);
        }

        public void Commit()
        {
            factory.CommitTransaction();
        }

        public void Rollback()
        {
            factory.RollbackTransaction();
        }

        //~UnitOfWork() => this.Dispose();

        public void Dispose()
        {
            factory.Dispose();
        }
    }
}
